<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Output_excel extends CI_Controller {

	function __construct() {
		parent::__construct();
		/*
		 $this->load->library('PHPExcel');
		 //$this->load->library('PHPExcel/PHPWorksheet');
		 $this->load->library('PHPExcel/PHPExcelWorksheetIterator');
		 $this->load->library('PHPExcel/IOFactory');
		 $this->load->library('PHPExcel/PHPStyle');
		 //$this->load->library('PHPExcel/PHPCell');
		 $this->load->model('aexcel_model',"do_excel");
		 */
	}

	function index() {
		
		phpinfo();
	}

	function member_excel() {

		@session_start();
		if (!empty($_POST["client"])) {
			$client = $_SESSION["client_page"];
			//$_POST["client"];
		}
		//print_r($_SESSION);

		$GG_EXC = true;

		if ($GG_EXC) {

			$query_users = $this -> do_excel -> get_users($client);

			$objPHPExcel = new PHPExcel();
			$objPHPExcel -> getProperties() -> setTitle("export") -> setDescription("none");
			$objPHPExcel -> setActiveSheetIndex(0);

			// Field names in the first row
			$fields = $this -> do_excel -> get_users_fields();
			//$query->list_fields();
			$col = 0;

			//設定抬頭
			$objPHPExcel -> getActiveSheet(0) -> getStyle("A1:CY1") -> applyFromArray(array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array("argb" => "D1EEEE")), ));

			$objPHPExcel -> getDefaultStyle() -> getFont() -> setSize(18);

			$objPHPExcel -> setActiveSheetIndex(0);

			$show_tag_name = $this -> users_files();
			$row = 1;
			foreach ($fields as $field) {

				if (!empty($show_tag_name[$field])) {

					$objPHPExcel -> getActiveSheet() -> setCellValueByColumnAndRow($col, $row, $show_tag_name[$field]);

				} else {

					$objPHPExcel -> getActiveSheet() -> setCellValueByColumnAndRow($col, $row, $field);
				}

				$col++;

			}

			//設定寛度
			for ($col = 'A'; $col != 'Z'; $col++) {

				$objPHPExcel -> getActiveSheet() -> getColumnDimension($col) -> setAutoSize(true);
				$objPHPExcel -> getActiveSheet() -> getColumnDimension("A" . $col) -> setAutoSize(true);
				$objPHPExcel -> getActiveSheet() -> getColumnDimension("B" . $col) -> setAutoSize(true);

			}

			// Fetching the table data
			$row = 2;
			foreach ($query_users->result() as $cir) {

				$col = 0;
				foreach ($fields as $field) {

					if ($field == "sex") {

						$sex = "";
						if ($cir -> $field == 1) {
							$sex = "男";
						} else {
							$sex = "女";
						}

						$objPHPExcel -> getActiveSheet() -> setCellValueByColumnAndRow($col, $row, $sex);

					} else {

						$objPHPExcel -> getActiveSheet() -> setCellValueByColumnAndRow($col, $row, $cir -> $field);
					}

					$col++;
				}

				$row++;
			}

			$objPHPExcel -> setActiveSheetIndex(0);

			$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');

			// Sending headers to force the user to download the file
			header('Content-Type: application/vnd.ms-excel');
			header('Content-Disposition: attachment;filename="Products_' . date('dMy') . '.xls"');
			header('Cache-Control: max-age=0');

			$objWriter -> save('php://output');
		}

	}

	public function users_files() {

		$show_tag_name = array("name" => "姓名", "english_name" => "英文名字", "sex" => "姓別", "recommended" => "", "importance" => "", "nationality" => "", "customer_source" => "", "remark" => "", "mobile" => "手機", "mobile1" => "手機1", "mobile2" => "手機2", "send_sms" => "", "send_sms1" => "", "send_sms2" => "", "home_phone" => "家用電話", "home_phone1" => "家用電話1", "home_phone2" => "家用電話2", "company_phone" => "公司電話", "company_phone_ext" => "", "company_phone1" => "公司電話1", "company_phone_ext1" => "", "company_phone2" => "公司電話2", "company_phone_ext2" => "", "email" => "電子信箱", "send_email" => "", "email1" => "電子信箱1", "send_email1" => "", "email2" => "電子信箱2", "send_email2" => "", "fax" => "", "fax1" => "", "fax2" => "", "home_address" => "家裡住址", "home_building_name" => "", "home_send_publication" => "", "home_address1" => "家裡住址1", "home_building_name1" => "", "home_send_publication1" => "", "home_address2" => "家裡住址2", "home_building_name2" => "", "home_send_publication2" => "", "company_name" => "公司名稱", "type_of_company" => "", "unicode" => "", "department" => "部門", "title" => "標題", "category" => "分類", "company_address" => "公司住址", "company_building_name" => "", "company_send_publication" => "", "company_website" => "公司網頁", "company_name1" => "公司名稱", "type_of_company1" => "", "unicode1" => "", "department1" => "部門", "title1" => "標題", "category1" => "分類", "company_address1" => "公司住址", "company_building_name1" => "", "company_send_publication1" => "", "company_website1" => "公司網頁2", "company_name2" => "公司名稱2", "type_of_company2" => "", "unicode2" => "", "department2" => "部門2", "title2" => "標題2", "category2" => "分類", "company_address2" => "公司住址2", "company_building_name2" => "", "company_send_publication2" => "", "company_website2" => "公司網頁2", "front_man" => "", "head" => "", "original_broker" => "", "broker" => "", "birthday" => "生日", "birthday_int" => "", "gifts" => "", "id_number" => "", "usually_contact" => "", "usually_start_time" => "", "usually_end_time" => "", "usually_contact1" => "", "usually_start_time1" => "", "usually_end_time1" => "", "usually_contact2" => "", "usually_start_time2" => "", "usually_end_time2" => "", "holidays_contact" => "", "holidays_start_time" => "", "holidays_end_time" => "", "holidays_contact1" => "", "holidays_start_time1" => "", "holidays_end_time1" => "", "holidays_contact2" => "", "holidays_start_time2 " => "", "holidays_end_time2" => "", "background_and_features" => "");

		return $show_tag_name;
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
